﻿<?php wp_reset_query(); ?>
<div class="sidebar">
    <!-- Widgets begin -->
    <ul>
    	<?php if ( is_page() ) { ?>
            <!-- SubPageList begin -->
            <?php
                $parent_page = $post->ID;
                while($parent_page) {
                    $page_query = $wpdb->get_row("SELECT ID, post_title, post_status, post_parent FROM $wpdb->posts WHERE ID = '$parent_page'");
                    $parent_page = $page_query->post_parent;
                 }
               $parent_id = $page_query->ID;
               $parent_title = $page_query->post_title;
                if ($wpdb->get_results("SELECT * FROM $wpdb->posts WHERE post_parent = '$parent_id' AND post_status != 'attachment'")) { ?>
                    <?php $subpage = wp_list_pages('depth=1&echo=0&child_of='.$parent_id); ?><?php //key ?>
                    <?php if($subpage) { ?>
                    	<li class="widget sublist">
                            <h3><?php echo $parent_title; ?></h3>
                            <ul><?php wp_list_pages('depth=1&sort_column=menu_order&title_li=&child_of='. $parent_id); ?></ul>
                    	</li>
					<?php } ?>
                <?php } ?>
            <!-- SubPageList end -->
        <?php } elseif( is_search() || is_404() ) { ?>
		
		<?php } else { ?>
        	<?php
				$this_category = get_the_category();
				$category_id = $this_category[0]->cat_ID;
				$parent_id = get_category_root_id( $category_id );
				$category_link = get_category_link( $parent_id );
        		$childcat = get_categories('child_of='.$parent_id);
				if( $childcat && $parent_id ){
			?>
            <!-- SubCatList begin -->
            <li class="widget sublist">
               <h3><?php echo get_cat_name( $parent_id ); ?></h3>
                <ul>
                    <?php wp_list_cats("orderby=id&child_of=" . $parent_id . "&depth=2&hide_empty=0"); ?>
                </ul>
            </li>
            <!-- SubCatList end -->
            <?php } else { ?><?php } ?>
        <?php } ?>
        <!-- LatestNews begin -->
        <li class="widget sublist">
           <h3>
           	<?php if (get_option('wpyou_news_id')) { $newsid = get_option('wpyou_news_id'); ?>
                <a href="<?php echo get_category_link( $newsid );?>" title="<?php echo get_cat_name( $newsid ); ?>"><?php echo get_cat_name( $newsid ); ?></a>
            <?php } else { ?>
                 <a href="<?php bloginfo('siteurl');?>/category/news">新闻动态</a>
            <?php } ?>
           </h3>
           <?php if (get_option('wpyou_news_id')) { $newsid = get_option('wpyou_news_id'); ?>
            <?php query_posts('caller_get_posts=1&showposts=6&cat='.$newsid); ?>
        <?php } else { ?>
            <?php query_posts('caller_get_posts=1&showposts=6&cat=news'); ?>
        <?php } ?>
        <ul>
        	<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
             <li><a href="<?php the_permalink() ?>" title="<?php the_title() ?>"><?php the_title() ?></a></li>
        	<?php endwhile; endif; ?>
        </ul>
        </li>
        <!-- LatestNews end -->
        <?php wp_reset_query(); ?>
        <!-- Search begin -->
        <li class="widget_search">
        	<h3>站内搜索</h3>
            <form method="get" id="searchform" action="<?php bloginfo('url'); ?>/">
                <input class="searchInput" type="text" value="输入关键字" name="s" id="s" />
                <input id="searchsubmit" class="searchBtn" type="submit" title="搜索" value="搜 索">
            </form>
        </li>
        
        <!-- Search begin -->
		<?php wp_reset_query(); ?>
    	<?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar(1) ) : else : ?>	
    		<!-- Search begin -->
            <li class="widget">
            	<h3>联系我们</h3>
            	<p><strong>公司地址</strong>: 安徽省长丰县岗集镇(231137)</p>
				<p><strong>联系电话</strong>: 0551-6779849</p>
                <p><strong>电子邮箱</strong>: 523165274@qq.com</p>                            
            
                <a href="<?php bloginfo('siteurl');?>/contact-us/"><img src="<?php bloginfo('template_directory');?>/images/contact-us.jpg" /></a>
            </li>
            <!-- Search begin -->
		<?php endif; ?>
    </ul>
    <!-- Widgets end -->
</div>
<?php wp_reset_query(); ?>